package com.bw.day03.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.net.entity.MyTabEntity
import com.bw.base.net.mvvm.BaseFragment
import com.bw.day03.R
import com.bw.day03.adapter.VpAdapter
import com.bw.day03.databinding.FragmentMineBinding
import com.bw.day03.order.Order1Fragment
import com.bw.day03.order.Order2Fragment
import com.bw.day03.order.Order3Fragment
import com.bw.day03.userinfo.UserViewModel
import com.bw.day03.util.GlideUtlis
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener

class MineFragment : BaseFragment<FragmentMineBinding, UserViewModel>() {
    val fglist=mutableListOf<Fragment>()
    val tabs=arrayListOf<CustomTabEntity>()
    lateinit var vpAdapter: VpAdapter
    override fun initView() {
        viewModel.user()
        fglist.add(Order1Fragment())
        fglist.add(Order2Fragment())
        fglist.add(Order3Fragment())
        vpAdapter= VpAdapter(requireActivity(),fglist)
        binding.vp.adapter=vpAdapter
        tabs.add(MyTabEntity("待付款",0,0))
        tabs.add(MyTabEntity("待发货",0,0))
        tabs.add(MyTabEntity("售后/退款",0,0))
        binding.common.setTabData(tabs)
        binding.vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.common.currentTab=position
            }
        })
        binding.common.setOnTabSelectListener(object : OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                binding.vp.currentItem=position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        binding.userPhone.setOnClickListener {
            ARouter.getInstance().build("/app/MainActivity2").navigation()
        }
    }

    override fun initData() {
        viewModel.userSuccess.observe(this){
            if (it.code==200){
                GlideUtlis.loadCircle(requireActivity(),it.data.avatar,binding.userIv)
                binding.userName.text=it.data.nickname
                binding.userPhone.text=it.data.phone
            }
        }

    }

    override fun getViewModel(): Class<UserViewModel> =  UserViewModel::class.java

    override fun getLayoutId(): Int =  R.layout.fragment_mine

}